# Load google apps modules
import os
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
from Functions import *


# PageMaker class implementation
class PageMaker(webapp.RequestHandler):
	def get(self):							
		# Get the page requested
		pages = ['home', 'work', 'contact', 'home_new']	
		page = self.request.path[1:]
		if page == '':
			page = 'home'				
		
		if page in pages:				
			if page == 'home':
				# Output page content
				content = {'title': 'Matthew Eberhardt',
							'content_file': 'pages/home.html'}
				
			elif page == 'home_new':
				# Output page content
				content = {'title': 'Matthew Eberhardt',
							'content_file': 'pages/home_new.html'}
				
			elif page == 'contact':		
				# Output page content
				content = {'title': 'Matthew Eberhardt :: Contact',
							'content_file': 'pages/contact.html'}	
				
			elif page == 'work':		
				# Get show page reference
				production = getVar(self, 'production')
				
				# Select page to output
				if production == '':
					content_file = 'pages/work.html'
				else:
					content_file = 'productions/' + production + '.html'
				
				# Output page content
				content = {'title': 'Matthew Eberhardt :: Work',
							'content_file': content_file,
							'production': production}
			
		else:
			# 404 error
			content = {'title': 'Matthew Eberhardt :: Page Not Found',
						'content_file': 'pages/404.html'}

		# Serve page
		path = os.path.join(os.path.dirname(__file__), 'shell.html')
		self.response.out.write(template.render(path, content))
		
	def post(self):
		# Get requested page
		page = self.request.path[1:]

		if page == 'contact':
			name = cgi.escape(self.request.get('name'))
			email = cgi.escape(self.request.get('email'))
			message = cgi.escape(self.request.get('message'))

			mail.send_mail(sender="Contact Form <tom@joebunker.co.uk>",
							  to="Matthew Eberhardt <matthew@mattheweberhardt.com>",
							  subject=name + ' has sent you a message via mattheweberhardt.com',
							  body='Email: ' + email +'\nMessage:\n\n' + message + '\n\n(do not press "reply" to this email - use the address given in the message!)')

			# Assemble output
			messageSent = []
			messageSent.append('<div id="messageSent">')
			messageSent.append('<p><b>Message sent!</b></p>')
			messageSent.append('<p><b>From:</b> ' + cgi.escape(self.request.get('name')) + ' (' + cgi.escape(self.request.get('email')) + ')</p>')
			messageSent.append('<p><b>Message:</b></p>')
			messageSent.append('<p>' + cgi.escape(self.request.get('message')) + '</p>')
			messageSent.append('</div>')

			# Get file name
			content_file = 'pages/contact.html'

			# Output page content
			content = {'title': 'Matthew Eberhardt :: Contact',
						'content_file': content_file,
						'message': messageSent}
		
		else:
			# 404 error
			content = {'title': 'Matthew Eberhardt :: Page Not Found',
						'content_file': 'pages/404.html'}

		# Serve page
		path = os.path.join(os.path.dirname(__file__), 'shell.html')
		self.response.out.write(template.render(path, content))
